package com.qzyh.esb.egsa.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qzyh.esb.egsa.bean.vo.*;
import com.qzyh.esb.egsa.service.IUserService;
import com.qzyh.esb.egsa.utils.PageResultObject;
import com.qzyh.esb.egsa.utils.ResultObject;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author whq
 * @since 2023-03-13
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService userService;

    @GetMapping("/list")
    @ApiOperation(value = "查询用户列表")
    public PageResultObject<UserVO> list(@ApiParam(value = "页码") @RequestParam("pageNum") Long pageNum,
                                                @ApiParam(value = "每页条数") @RequestParam("pageSize") Long pageSize,
                                                @ApiParam(value = "用户名") @RequestParam("userName") String userName) {
        IPage<UserVO> page = new Page<>(pageNum==null?0:pageNum, pageSize==null?10:pageSize);
        page = userService.getList(page,userName);
        PageResultObject<UserVO> pageResultObject = PageResultObject.success();
        pageResultObject.setPageNum(page.getCurrent());
        pageResultObject.setPageSize(page.getSize());
        pageResultObject.setTotal(page.getTotal());
        pageResultObject.setData(page.getRecords());
        return pageResultObject;
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存用户信息")
    public ResultObject save(@RequestBody UserSaveVO vo) {
        logger.info("请求参数："+vo);
        return userService.saveUserInfo(vo);
    }

    @GetMapping("/delete")
    @ApiOperation(value = "根据id删除用户信息")
    public ResultObject deleteById(@ApiParam(value = "用户id") @RequestParam("id") String id) {
        logger.info("删除用户信息id："+id);
        return userService.deleteById(id);
    }

    @GetMapping("/getUserRoleAndDeptById")
    @ApiOperation(value = "根据id获取用户角色和部门信息")
    public ResultObject<UserRoleAndDeptVO> getUserRoleAndDeptById(@ApiParam(value = "用户id") @RequestParam("id") String id) {
        logger.info("用户信息id："+id);
        return userService.queryUserRoleAndDeptById(id);
    }

    @PostMapping("/changePwd")
    @ApiOperation(value = "修改密码")
    public ResultObject changePwd(@RequestBody ChangePwdVO vo) {
        logger.info("请求参数："+vo);
        return userService.changePwd(vo);
    }

    @GetMapping("/resetPwd")
    @ApiOperation(value = "根据id重置用户密码")
    public ResultObject resetPwd(@ApiParam(value = "用户id") @RequestParam("id") String id) {
        logger.info("重置密码，用户信息id："+id);
        return userService.resetPwd(id);
    }
}
